package org.onionframework.persistence.dao.processor;

import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.onionframework.persistence.dao.CriteriaBuilder;
import org.onionframework.persistence.dao.PropertyFilterProcessor;
import org.onionframework.persistence.dao.expression.OnionExpressions;

public class DateStringProcesser implements PropertyFilterProcessor{

	private String op;
	
	public DateStringProcesser(String op){
		this.op = op;
	}
	
	@Override
	public Criterion buildCriterion(String alias, String name, Object value,
			CriteriaBuilder context) {
		String[] values = (String[])value;
		if("eq".equals(op)){
			if(values[1] == null)
				return Restrictions.isNull(alias);
			else
				return OnionExpressions.dateStringEq(alias, values[1], values[0]);
		}else if("like".equals(op)){
			return OnionExpressions.dateStringLike(alias, values[1], values[0]);
		}else
			throw new RuntimeException("不可识别的操作符：" + op);
	}

}
